
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Runtime.Serialization;
using System.Text;

using Swaf.Call;
using Swaf.Resource;

namespace Swaf
{
	/// <summary>
	/// Summary description for SingleCallAndResult.
	/// </summary>
	public class SingleCallAndResult : MarshalByRefObject, ICallResults
	{
		protected ICall m_call = null;
		protected object m_callResults = null;
		protected Exception m_callException = null;

		protected static string s_baseXmlFormat = 
			"<RuleCallResults>{0}</RuleCallResults>";
		protected static string s_baseResultXmlFormat =
			"<CallResults>{0}</CallResults>";
		protected static string s_baseExceptionXmlFormat =
			"<Exception>{0}</Exception>";
		protected static int s_initialBuffSize = 512;

		public SingleCallAndResult(ICall call)
		{
			m_call = call;
		}
		public SingleCallAndResult(object callResults, Exception err)
		{
			setResults(callResults, err);
		}

		public void setResults(object callResults, Exception err)
		{
			m_callResults = callResults;
			m_callException = err;
		}

		public void setCallInfo(ICall call)
		{
			m_call = call;
		}

		#region ICallResults Members
		public ICall CallInfo {get{return m_call;}}
		#endregion

		#region IResults Members
		public object CallResults {get{return m_callResults;}}
		public Exception ExceptionInfo {get{return m_callException;}}
		public int numberOfResults {get{return 1;}}
		public object getCallResults(int index)	{return m_callResults;}
		public Exception getExceptionInfo(int index) {return m_callException;}

		public string getXml()
		{
			IResourceManager resMgr = Application.currentApp.resMgr;
			StringBuilder buf = new StringBuilder(s_initialBuffSize);
			buf.Append("<RuleCallResults>\n");
			
			if(CallResults != null)
			{
				SerializationInfo sinfo = Serialization.serialize(CallResults);
				buf.Append("<CallResult type='").Append(sinfo.TypeName).Append("'>");
				buf.Append(resMgr.fixupForXml(sinfo.SerialObject));
				buf.Append("</CallResult>\n");
			}
			else if(ExceptionInfo != null)
			{
				buf.Append("<Exception>\n").Append(resMgr.fixupForXml(ExceptionInfo.ToString())).Append("</Exception>\n");
			}
			buf.Append("</RuleCallResults>\n");
			return buf.ToString();
		}
		#endregion
	}
}
